// pages/go-jianbao/jianbao.js
import {
  ajax,
  src,
  util
} from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    circular: true,
    color: '#E7E4E2',
    activecolor: '#fff',
    interval: 3000,
    duration: 500,
    bidshow: false,
    succeedshow: false,
    banners: '',
    islogin: false,
    auction_img: '',
    spike_img: '',
    jianbao_img: '',
    forum_img: '',
    zeroPattime: '',
    zeroPatshi: '',
    zeroPatfen: '',
    zeroPatmiao: '',
    timeLimittime: '',
    timeLimitshi: '',
    timeLimitfen: '',
    timeLimitmiao: '',
    zeroPatList: '',
    timeLimitList: '',
    guize: '',
    chujiaindex: '',
    lingxianjia: 0,
    verify: '',
    id: '',
    timer1:"",
    timer2:'',
    timerlunxun:"",

    chenggonglist:'',
    chenggongshow:1,

    showyajin:false,
    yjid:"",
    first:true,

  },
  zixunfenxiang:function(){

  },
  //退出出家弹窗
  tcchujiatc:function(){
    this.setData({
      bidshow:false
    })
  },



  // 授权登录
  bindGetUserInfo(e) {
    var that = this;
    if (wx.getStorageSync("uqid")) {
      this.setData({
        islogin: true
      })
    } else {
      if (e.detail.userInfo) {
        wx.setStorageSync("nickName", e.detail.userInfo.nickName)
        wx.setStorageSync("avatarUrl", e.detail.userInfo.avatarUrl)
        console.log()
        // 用户按了允许授权按钮
        var code = wx.getStorageSync("code");
        ajax('/api/user/login', {
          data: {
            code: code,
            nickname: e.detail.userInfo.nickName,
            avatar: e.detail.userInfo.avatarUrl,
          },
          header: {
            'Accept': 'application/x-www-form-urlencoded', // 默认值
          },
          method: "put",
        }, res => {
          wx.setStorageSync("uqid", res.data.uqid)
          this.setData({
            islogin: true
          })
        })
      } else {
        wx.showToast({
          title: '请先授权！',
          icon: 'none',
          duration: 2000
        })
      }
    }

  },

  //退出押金弹窗
  tctcyj:function(){
    this.setData({
      showyajin:false
    })
  },

  //查询押金金额
  chajine:function(){
    ajax('/api/user/check_auction_deposit', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        goods_id: this.data.yjid
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      this.setData({ yjje: res.data.margin})
    })
  },

  // 去交押金
  topayyajin:function(e){
    var id=e.currentTarget.dataset.id
    console.log(e)
    this.setData({
      showyajin:true,
      yjid:id
    })

    this.chajine()
  },

  queding:function(id){
    this.surepay()
  }, 

  // 交押金
  surepay:function(){
    this.setData({
      showyajin:false
    })
    ajax('/api/mall/deposit_pay', {
      data: {
        uqid:wx.getStorageSync("uqid"),
        goods_id: this.data.yjid
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "post",
    }, res => {
      if (res.error == 0) {
        wx.requestPayment({
          timeStamp: res.data.sin_data.timeStamp,
          nonceStr: res.data.sin_data.nonceStr,
          package: res.data.sin_data.package,
          signType: res.data.sin_data.signType,
          paySign: res.data.sin_data.paySign,

          success(res) {
            console.log(res)
            this.getzeropatlist()
          },
          fail(res) {
            console.log(res)
          }
        })
      } else if (res.error == 200) {
        wx.showToast({
          title: '押金支付成功',
          icon: 'succes',
          duration: 1000,
          mask: true
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'succes',
          duration: 1000,
          mask: true
        })
      }
    })
  },

  // 搜索
  search: function () {
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  // 获取轮播图
  getbanner: function () {
    ajax('/api/index/banner', {
      data: {
        type: 1
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      this.setData({
        banners: res.data.list
      })
    })
  },
  // 轮播图跳转
  tiaozhuan: function (e) {
    console.log(e)
    var key = e.currentTarget.dataset.key;
    var id = e.currentTarget.dataset.id;
    if (key == 'good_list') {
      //跳商品转列表
      wx.navigateTo({
        url: '/pages/shopping/shop',
      })
    } else if (key == 'good_show') {
      // 商品详情
      wx.navigateTo({
        url: '/pages/shop-details/details?id=' + id,
      })
    } else if (key == 'period_list') {
      // 跳转秒杀列表
      wx.navigateTo({
        url: '/pages/time-seckill/seckill',
      })
    } else if (key == 'period_show') {
      // 跳转秒杀详情
      wx.navigateTo({
        url: '/pages/shop-details-seckill/seckill?id=' + id,
      })
    } else if (key == 'auction_list') {
      // 跳转竞拍列表
      wx.navigateTo({
        url: '/pages/zero-yuan-clap/clap',
      })
    } else if (key == 'auction_show') {
      // 跳转竞拍详情
      wx.navigateTo({
        url: '/pages/zero-yuan-clap-details/details?id=' + id,
      })
    }
  },
  // 0元拍/秒杀/鉴宝/论坛-顶部图/优势图
  getways: function () {
    ajax('/api/index/config_list', {
      data: {
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {

      this.setData({
        auction_img: res.data.auction_style,
        spike_img: res.data.spike_style,
        jianbao_img: res.data.jianbao_style,
        forum_img: res.data.forum_style,
      })
    })
  },
  // 0元拍 剩余时间
  getzeroPattime: function () {
    ajax('/api/index/activity_time', {
      data: {
        type: 2
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      if (res.data.hint == "0") {
        this.setData({
          zeroPattime: "0:0:0",
          zerostatus: res.data.hint
        })
      } else {
        this.setData({
          zeroPattime: res.data.time_diff,
          zerostatus: res.data.hint
        })

        this.altertime()
      }

      
    })
  },
  // 限时秒杀 剩余时间
  gettimeLimittime: function () {
    ajax('/api/index/activity_time', {
      data: {
        type: 1
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      if (res.data.hint == "0") {
        this.setData({
          timeLimittime: "0:0:0",
          timeLimitstatus: res.data.hint
        })
      } else {
        this.setData({
          timeLimittime: res.data.time_diff,
          timeLimitstatus: res.data.hint
        })

        this.altertimes()
      }

      
    })
  },

  // 定时器  一秒走一次
  settime: function () {
    var that = this
    that.data.timer1 = setInterval(function () {
      var miao = that.data.zeroPatmiao -= 1
      var fen = that.data.zeroPatfen
      var shi = that.data.zeroPatshi
      // console.log(miao, fen, shi, 'shifenmiao')
      if (miao <= 0) {
        fen -= 1
        miao = 60
      }
      if (fen == -1) {
        shi -= 1
        fen = 60
      }

      that.setData({
        zeroPatmiao: miao,
        zeroPatfen: fen,
        zeroPatshi: shi
      })
      if (shi == -1) {
        clearInterval(that.timer1)
        console.log("计时结束")
        setTimeout(function(){
          that.getzeroPattime()
        },1000)
      }
    }, 1000)
  },
  // 定时器  一秒走一次
  settimes: function () {
    var that = this
    that.data.timer2 = setInterval(function () {
      var miao = that.data.timeLimitmiao -= 1
      var fen = that.data.timeLimitfen
      var shi = that.data.timeLimitshi
      if (miao <= 0) {
        fen -= 1
        miao = 60
      }
      if (fen == -1) {
        shi -= 1
        fen = 60
      }

      console.log("计时zhong")

      that.setData({
        timeLimitmiao: miao,
        timeLimitfen: fen,
        timeLimitshi: shi
      })

      if (shi == -1) {
        clearInterval(that.timer2)
        console.log("计时结束")
        setTimeout(function(){
          that.gettimeLimittime()
        },1000)
      }

    }, 1000)
  },
  // 分隔  时分秒  剩余时间  
  altertime: function () {
    var zeroPattime = this.data.zeroPattime;
    var arr = zeroPattime.split(":");
    this.setData({
      zeroPatshi: arr[0],
      zeroPatfen: arr[1],
      zeroPatmiao: arr[2]
    })
    this.settime()
  },
  // 分隔  时分秒  剩余时间  
  altertimes: function () {
    var timeLimittime = this.data.timeLimittime;
    var arr2 = timeLimittime.split(":");
    this.setData({
      timeLimitshi: arr2[0],
      timeLimitfen: arr2[1],
      timeLimitmiao: arr2[2]
    })
    this.settimes()
  },
  // 平台规则
  guize: function () {
    ajax('/api/index/theorem', {
      data: {
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      this.setData({
        guize: res.data
      })
    })
  },
  cha:function(){
    this.setData({
      chenggongshow:3,
    })
  },
  showtx:function(){

  },

  // 0元拍卖列表 
  getzeropatlist: function () {
    ajax('/api/index/bottom_auction_list', {
      data: {
        type: 3
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      this.setData({
        zeroPatList: res.data
      })
    })
  },

  //定时任务

  chaxunwzf:function(){

    ajax('/api/user/is_auction_pay', {
      data: {
        uqid: wx.getStorageSync('uqid')
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      if (res.data.status == 1){
        this.setData({
          chenggonglist: res.data.order_list[0],
          
        })
        if (this.data.first == true) {
          this.setData({
            chenggongshow: 2,
            first:false
          })
        }
      }else{
        this.setData({
          chenggongshow:1
        })
      }
      
    })

  },
  showtx:function(){
    this.setData({
      chenggongshow:2
    })
  },

  lunxun:function(){
    var that=this
    that.data.timerlunxun=setInterval(function(){
      that.getzeropatlist()

      if (that.data.islogin){
        that.chaxunwzf()
      }
    },5000)
  },



  // 限时秒杀列表
  gettimelimitlist: function () {
    ajax('/api/index/bottom_auction_list', {
      data: {
        type: 2
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      this.setData({
        timeLimitList: res.data
      })
    })
  },




  // 显示出价矿
  bid: function (e) {
    var chujiaindex = e.currentTarget.dataset.index
    var id = e.currentTarget.dataset.id
    // 查询是否交押金
    ajax('/api/index/is_deposit', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        goods_id: id
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "get",
    }, res => {
      // 0 - 未支付 直接去调用支付押金接口
      // 1 - 已支付 
      if (res.data.status == 1) {

        this.setData({
          bidshow: true,
          chujiaindex: chujiaindex,
          id: id
        })

      } else {
        // wx.showToast({
        //   title: '先支付押金',
        //   icon: 'succes',
        //   duration: 1000,
        //   mask: true
        // })
        // 押金支付
        this.setData({
          showyajin:true,
          yjid: id
        })
        this.chajine()
      }
    })

  },
  
  // 获取出价
  valueInput: function (e) {
    console.log(e)
    var reg = /^[0-9]+.?[0-9]*$/;
    if (reg.test(e.detail.value)) {
      var qian = parseFloat((e.detail.value - this.data.zeroPatList[this.data.chujiaindex].price).toFixed(10))
      this.setData({
        verify: e.detail.value,
        lingxianjia: qian
      });
    }

  },
  // 确认出价
  surebid: function () {
    this.setData({
      bidshow: false,
    })
    ajax('/api/mall/bid_add', {
      data: {
        uqid: wx.getStorageSync("uqid"),
        goods_id: this.data.id,
        price: this.data.verify
      },
      header: {
        'Accept': 'application/x-www-form-urlencoded', // 默认值
      },
      method: "post",
    }, res => {
      if (res.error == 0) {
        this.setData({

          // succeedshow: true,
          verify: 0,
          lingxianjia: 0

        })

        wx.showToast({
          title: '出价成功',
          duration: 1000,
          mask: true
        })

      } else {
        wx.showToast({
          title: res.msg,
          duration: 1000,
          mask: true
        })
        this.setData({
          verify: 0,
          lingxianjia: 0
        })
      }

    })
  },
  // 回到首页出价成功矿
  quxiao: function () {
    this.setData({
      succeedshow: false
    })
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },


  // 查看竞拍出价成功框
  queren: function () {
    this.setData({
      succeedshow: false
    })
    wx.navigateTo({
      url: '/pages/my-auction/auction',
    })
  },

  chakanxiangqing:function(e){
    var select=e.currentTarget.dataset.select
    wx.navigateTo({
      url: '/pages/my-auction/auction?select=' + select,
    })
  },
  // go鉴宝
  gojianbao: function () {
    wx.navigateTo({
      url: '/pages/all-jianbao/jianbao',
    })
  },
  // go社区
  goshequ: function () {
    console.log(123)
    wx.switchTab({
      url: '/pages/all-community/community',
    })
  },
  // go翡翠学院
  gocollege: function () {
    wx.navigateTo({
      url: '/pages/jade-college/college',
    })
  },
  // go平台规则
  gorulelist: function () {
    wx.navigateTo({
      url: '/pages/platform-rule-list/list',
    })
  },
  // 0元拍  查看更多
  zeromore: function () {
    wx.navigateTo({
      url: '/pages/zero-yuan-clap/clap',
    })
  },
  // 限时秒杀  查看更多
  goseckill: function () {
    wx.navigateTo({
      url: '/pages/time-seckill/seckill',
    })
  },
  // 0元拍 详情
  gozerodetails: function (e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/zero-yuan-clap-details/details?id=' + id,
    })
  },
  // 限时秒杀 详情
  goseckilldetails: function (e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/shop-details-seckill/seckill?id=' + id,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {


    this.setData({
      chenggongshow: 1,
      first: true
    })


    var uqid = wx.getStorageSync("uqid")
    if (uqid) {
      this.setData({
        islogin: true
      })
      
    }
    this.getbanner()
    this.getways()
    this.getzeroPattime();
    this.gettimeLimittime();
    this.guize();
    this.getzeropatlist();
    this.gettimelimitlist();

    this.lunxun()


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(this.data.timer1)
    clearInterval(this.data.timer2)
    clearInterval(this.data.timerlunxun)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.data.timer1)
    clearInterval(this.data.timer2)
    clearInterval(this.data.timerlunxun)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    clearInterval(this.data.timer1)
    clearInterval(this.data.timer2)



    this.getbanner()
    this.getways()
    this.getzeroPattime();
    this.gettimeLimittime();
    this.gettimelimitlist();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})